package cn.zy.jboa.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.zy.jboa.dao.CommonDAO;
import cn.zy.jboa.entity.BizLeave;
import cn.zy.jboa.service.BizLeaveService;

@Service("bizLeaveService")
public class BizLeaveServiceImpl implements BizLeaveService {
	@Autowired
	@Qualifier("commonDAOImpl")
	CommonDAO commonDao;
	
	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}

	@Override
	public List leaveList(String id,String position,int first, int size,
						String startDate,String endDate) {
		StringBuffer hql=new StringBuffer("from BizLeave bl LEFT JOIN FETCH bl.sysEmployeeByEmployeeSn LEFT JOIN FETCH bl.sysEmployeeByNextDealSn where 1=1");
		if(StringUtils.isNotBlank(startDate)){
			hql.append(" and '"+startDate+"'<= bl.createtime ");
		}
		if(StringUtils.isNotBlank(endDate)){
			hql.append(" and '"+endDate+"'>= bl.createtime ");
		}
		if("员工".equals(position)){
			hql.append(" and bl.sysEmployeeByEmployeeSn.sn='"+id+"'");
		}else if("部门经理".equals(position)){
			hql.append(" and bl.sysEmployeeByNextDealSn.sn='"+id+"'");
		}
		hql.append(" order by bl.createtime desc");
		return (List) commonDao.query(hql.toString(), first, size);
	}

	@Override
	public int getCount(String sn, String position, String startDate,
			String endDate) {
		StringBuffer hql=new StringBuffer("select count(id) from BizLeave");
		if("员工".equals(position)){
			hql.append(" where sysEmployeeByEmployeeSn.sn='"+sn+"'");
		}else if("部门经理".equals(position)){
			hql.append(" where sysEmployeeByNextDealSn.sn='"+sn+"'");
		}else{
			hql.append(" where ");
		}
		if(StringUtils.isNotBlank(startDate)){
			hql.append(" and '"+startDate+"'<= createtime ");
		}
		if(StringUtils.isNotBlank(endDate)){
			hql.append(" and '"+endDate+"'>= createtime ");
		}
		int count = Integer.parseInt(commonDao.count(hql.toString()).toString());
		return count ;
	}

	@Override
	public BizLeave getLeaveId(int id) {
		return (BizLeave) commonDao.get(BizLeave.class, id);
	}

	@Override
	public void updateLeave(BizLeave leave) {
		commonDao.update(leave);
	}

	@Override
	public int saveLeave(BizLeave leave) {
		int ret = (Integer) commonDao.add(leave);
		System.out.println(ret);
		return ret;
	}
}
